// pages/mine/info/dlc.js
const App = getApp()
import Util from '../../../utils/util'

Page({
  data: {
    init: false,

    auth: {},

    dlca: '',
    dlcb: '',
    name: '',
    fno: '',
    sno: '',
    type: '',
  },
  onLoad() {
    this.execLoad()
  },
  execLoad() {
    App.doPost({
      uri: '/app/user/auth/dlc/info',
      body: {},
      fnSucc: resp => {
        this.setData({
          init: true,
          auth: resp.data,
          dlca: resp.data.dlc.pica,
          dlcb: resp.data.dlc.picb,
          name: resp.data.dlc.name,
          fno: resp.data.dlc.fno,
          sno: resp.data.dlc.sno,
          type: resp.data.dlc.type
        })
      }
    })
  },
  doCertChange(e) {
    let type = e.target.dataset.type
    let odata = {}
    odata[type] = e.detail.url

    if (type == 'dlca') {
      if (e.detail.ocr.name && e.detail.ocr.type) {
        odata["name"] = e.detail.ocr.name
        odata["type"] = e.detail.ocr.type
        odata["sno"] = e.detail.ocr.sno
      }
    }
    if (type == 'dlcb') {
      if (e.detail.ocr.fno) {
        odata["fno"] = e.detail.ocr.fno
      }
    }
    this.setData({
      ...odata
    })
  },

  execSave() {
    if (!this.data.dlca) {
      Util.Notify.warn(`请上传驾驶证正本`)
      return
    }
    if (!this.data.dlcb) {
      Util.Notify.warn(`请上传驾驶证副本`)
      return
    }
    if (!this.data.name) {
      Util.Notify.warn(`请填写驾驶员姓名`)
      return
    }
    if (!this.data.sno) {
      Util.Notify.warn(`请填写驾驶证号`)
      return
    }
    if (!this.data.fno) {
      Util.Notify.warn(`请填写档案编号`)
      return
    }
    this.setData({
      loading: true
    })
    App.doPost({
      uri: '/app/user/auth/dlc/submit',
      body: {
        dlca: this.data.dlca,
        dlcb: this.data.dlcb,
        name: this.data.name,
        sno: this.data.sno,
        fno: this.data.fno,
        type: this.data.type
      },
      fnSucc: () => {
        wx.pub("ReloadUserInfo", {})
        Util.Toast.succ(`提交成功`)
        setTimeout(() => {
          wx.navigateBack({
            delta: 1
          })
        }, 1000);
      },
      fnFail: resp => {
        Util.Notify.error(`[${resp.stat.err}] ${resp.stat.msg}`)
      },
      fnComp: () => {
        this.setData({
          loading: false,
        })
      }
    })
  }
})